using ET;

namespace Core
{
    [Event]
    public class CurrentItemSlostChangeEvent : AEvent<CurrentItemSlostChange>
    {
        protected override async ETTask Run(CurrentItemSlostChange swap)
        {
            var inventory = swap.unit.GetComponent<InventoryComponent>();
            var fgui = World.GetComponent<FGUIComponent>();
            var window = await fgui.GetWindowAsync<MainWindow>();

            if(swap.oldSlot != null)
            {
                if (inventory.backpack == swap.oldSlot.container)
                {
                    window.inventoryWidget.SelectSlot(swap.oldSlot.index, false);
                }
                else if (inventory.equipment == swap.oldSlot.container)
                {
                    window.roleInfoWidget.SelectSlot(swap.oldSlot.index, false);
                }
            }

            if (swap.newSlot != null)
            {
                if (inventory.backpack == swap.newSlot.container)
                {
                    window.inventoryWidget.SelectSlot(swap.newSlot.index, true);
                }
                else if (inventory.equipment == swap.newSlot.container)
                {
                    window.roleInfoWidget.SelectSlot(swap.newSlot.index, true);
                }
            }
        }
    }
}